package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdPressureElementInfoDao;
import com.ccp.dev.qbdms.model.WQbdPressureElementInfo;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 压力元件 信息操作服务类
 *
 * @author milihao
 * @date 2020-07-06 09:24:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdPressureElementInfoService extends BaseService<WQbdPressureElementInfo> {

	@Resource
	private WQbdPressureElementInfoDao dao;


	public List<WQbdPressureElementInfo> reportList(String id, String tableName) {
		return dao.reportList(id, tableName);
	}

	public List<WQbdPressureElementInfo> queryAll(QueryFilter queryFilter) {
		return dao.queryAll(queryFilter);
	}

	public void addImportData(List<WQbdPressureElementInfo> list) {
		ZhiLiangConst.addCommonFields(list);
		//上报状态
		list.forEach(a -> {
			String mainId = ProjectUUIDUtils.getUUIDFor32();
			a.setId(mainId);
			a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
		});
		if (!list.isEmpty()) {
			dao.addList(list);
		}
	}
	/**
	 * 根据主表id查询主表加子表数据
	 *
	 * @param id 主表id
	 * @return
	 */
	public WQbdPressureElementInfo getParentAndChilden(String id) {
		return dao.getParentAndChilden(id);
	}

	/**
	 * 保存接口传输的数据
	 *
	 * @param wQbdChemicalPhysicsInfoList 主表model,有子表的包含子表数据
	 * @return
	 */
		public void saveAll(List<WQbdPressureElementInfo> wQbdChemicalPhysicsInfoList,List<ExtendSysFile> extendSysFileList){
			ReadAttachmentUtil.getEntityData(wQbdChemicalPhysicsInfoList,extendSysFileList);
			for (WQbdPressureElementInfo model : wQbdChemicalPhysicsInfoList) {
				// 如果上报数据中hospital为空,认为是三级上报来的,如果不为空,则认为是二级上报来的
				if (StringUtil.isEmpty(model.getFilldeptHospitalId())) {
					model.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptid()));
				}
				else{
					model.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptHospitalId()));
				}
				if (null == dao.getById(model.getId())) {
					dao.add(model);
				} else {
					dao.update(model);
				}
			}
		}
}
